package trans;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * @ClassName Myframe
 * @Description TODO
 * @Author WSY
 * @Date 2020-6-3 17:19
 */

public class Myframe extends JFrame {
    private JTextField input;

    private LinkedList<Main> content = new LinkedList<>();

    private JButton add = new JButton("添加");

    JTextField cinput = new JTextField();

    JTextField einput = new JTextField();

    public Myframe() {
        Main r1 = new Main("age", "年龄");
        Main r2 = new Main("name", "姓名");
        Main r3 = new Main("hello", "你好");
        content.add(r1);
        content.add(r2);
        content.add(r3);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        JPanel contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        input = new JTextField();
        input.setBounds(37, 34, 235, 28);
        contentPane.add(input);
        input.setColumns(10);

        JTextArea output = new JTextArea();
        output.setBounds(37, 72, 235, 161);
        contentPane.add(output);
        JButton search = new JButton("search");
        search.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String str = input.getText();
                String regex = "[A-Za-z|\\u4e00-\\u9fa5]+";
                if (str.matches(regex)) {
                    Iterator<Main> it = content.iterator();
                    Iterator<Main> has = content.iterator();
                    Main re, ea;
                    boolean flag = true;
                    while (it.hasNext()) {
                        re = it.next();
                        if (re.english.equals(str)) {
                            flag = false;
                            output.setText(re.chinese);
                        }
                    }
                    while (has.hasNext()) {
                        ea = has.next();
                        if (ea.chinese.equals(str)) {
                            flag = false;
                            output.setText(ea.english);
                        }
                    }
                    if (flag) {
                        output.setText("no saerch");
                    }
                }

            }
        });
        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String english = einput.getText();
                String chinese = cinput.getText();
                content.add(new Main(english,chinese));
                JOptionPane.showMessageDialog(add,"添加成功","成功",JOptionPane.INFORMATION_MESSAGE);

            }
        });
        search.setBounds(310, 33, 100, 29);
        contentPane.add(search);
        add.setBounds(310, 200, 100, 29);
        contentPane.add(add);
        cinput.setBounds(310, 80, 100, 29);
        einput.setText("英");
        contentPane.add(cinput);
        einput.setBounds(310, 140, 100, 29);
        cinput.setText("中");
        contentPane.add(einput);
        setLocationRelativeTo(null);
    }

}